package com.lxlg.lxlg_order.service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxlg.core.utils.Result;
import com.lxlg.lxlg_order.client.param.SearchDayStatementFOrm;
import com.lxlg.lxlg_order.client.result.YearStatementInfo;
import com.lxlg.lxlg_order.infra.mapper.MonthStatementMapper;
import com.lxlg.lxlg_order.infra.mapper.YearStatementMapper;
import com.lxlg.lxlg_order.infra.model.DayStatement;
import com.lxlg.lxlg_order.infra.model.MonthStatement;
import com.lxlg.lxlg_order.infra.model.YearStatement;
import com.lxlg.lxlg_order.service.service.YearStatementService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author kevin
 * @since 2022-04-28
 */
@Service
public class YearStatementServiceImpl extends ServiceImpl<YearStatementMapper, YearStatement> implements YearStatementService {
    @Resource
    private MonthStatementMapper monthStatementMapper;

    @Scheduled(cron = "0 0 0 1 1 *")//每月一号 0:00:00
    public void collectYearStatement(){

        Date date = new Date();
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE,-20);
        Date date1 = calendar.getTime();
        calendar.add(Calendar.MONTH ,-1);
        Date date0=calendar.getTime();
        QueryWrapper<MonthStatement> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("month_statement_time",date0).le("month_statement_time",date1);
        List<MonthStatement> dayStatements = monthStatementMapper.selectList(queryWrapper);
        List<MonthStatement> out = new ArrayList<>();
        Map<Integer,MonthStatement> ls = new  HashMap<Integer,MonthStatement>();
        for (MonthStatement dayStatement : dayStatements) {
            MonthStatement dayStatement1 = ls.get(dayStatement.getSkuId());
            if(null==dayStatement1){
                ls.put(dayStatement.getSkuId(),dayStatement);
                out.add(dayStatement);
            }else {
                dayStatement1.setMonthStatementSalenum(dayStatement1.getMonthStatementSalenum()+dayStatement.getMonthStatementSalenum());
                dayStatement1.setMonthStatementPrice(dayStatement1.getMonthStatementPrice()+dayStatement.getMonthStatementPrice());
            }
        }

    }
    
    
    @Override
    public Result<List<YearStatementInfo>> queryYearStatement(SearchDayStatementFOrm searchDayStatementFOrm) {
        QueryWrapper<YearStatement> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("year_statement_time",searchDayStatementFOrm.getStartTime()).le("year_statement_time",searchDayStatementFOrm.getEndtTime());
        List<YearStatement> monthStatements = getBaseMapper().selectList(queryWrapper);
        return Result.getInstanceList(YearStatementInfo.class).setData(BeanUtil.copyToList(monthStatements,YearStatementInfo.class));

    }
}
